/*
 * Package: org.grassfield.common.view
 * FileName: UserAction.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package action;

import java.util.List;


import service.IMailerService;
import service.ITeamService;
import service.IUserRoleService;
import service.IUserService;
import util.Utilities;

import bean.Team;
import bean.User;
import bean.UserRole;

import com.opensymphony.xwork2.ActionSupport;


/**
 * The Class UserAction.
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
	private IUserService userService;
	private ITeamService teamService;
	private IUserRoleService userRoleService;
	private List<User> userList;
	private List<Team> teamList;
	private List<UserRole> userRoleList;
	private List<String> countryList;
	private User newUser;
	private IMailerService mailerService;
	
	/**
	 * Sets the user service.
	 * 
	 * @param userService the new user service
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * Gets the user list.
	 * 
	 * @return the user list
	 */
	public List<User> getUserList() {
		return userList;
	}

	/**
	 * Gets the new user.
	 * 
	 * @return the new user
	 */
	public User getNewUser() {
		return newUser;
	}

	/**
	 * Sets the new user.
	 * 
	 * @param newUser the new new user
	 */
	public void setNewUser(User newUser) {
		this.newUser = newUser;
	}

	/**
	 * Gets the team list.
	 * 
	 * @return the team list
	 */
	public List<Team> getTeamList() {
		return teamList;
	}

	/**
	 * Sets the team list.
	 * 
	 * @param teamList the new team list
	 */
	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	/**
	 * Gets the user role list.
	 * 
	 * @return the user role list
	 */
	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}

	/**
	 * Sets the user role list.
	 * 
	 * @param userRoleList the new user role list
	 */
	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}

	/**
	 * Sets the team service.
	 * 
	 * @param teamService the new team service
	 */
	public void setTeamService(ITeamService teamService) {
		this.teamService = teamService;
	}

	/**
	 * Sets the user role service.
	 * 
	 * @param userRoleService the new user role service
	 */
	public void setUserRoleService(IUserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

	/**
	 * Gets the country list.
	 * 
	 * @return the country list
	 */
	public List<String> getCountryList() {
		return countryList;
	}

	/**
	 * List.
	 * 
	 * @return the string
	 */
	public String list(){
		this.userList = this.userService.list();
		return "list";
	}
	
	/**
	 * Sets the mailer service.
	 * 
	 * @param mailerService the new mailer service
	 */
	public void setMailerService(IMailerService mailerService) {
		this.mailerService = mailerService;
	}

	/**
	 * Adds the.
	 * 
	 * @return the string
	 */
	public String add(){
		if (this.newUser!=null){
			this.newUser.setTeam(this.teamService.find(this.newUser.getTeam().getId()));
			this.newUser.setUserRole(this.userRoleService.find(this.newUser.getUserRole().getId()));
			this.newUser = this.userService.addUser(newUser);
			mailerService.setTo(this.newUser.getContactInfo().getEmail1());
			mailerService.setTitle("New user added - "+this.newUser.getUserName());
			if ("Yes".equals(this.newUser.getUserRole().getEmailPermission())){
				//no need to send emails to customers/resellers
				StringBuffer sb = new StringBuffer();
				sb.append("your credential for Bodado portal is given below<br>");
				sb.append("Username:");
				sb.append(this.newUser.getUserName());
				sb.append("<br>");
				sb.append("Password:");
				sb.append(this.newUser.getPassword());
				sb.append("<br>");
				sb.append("Role:");
				sb.append(this.newUser.getUserRole().getName());
				sb.append("<br>");
				sb.append("Team:");
				sb.append(this.newUser.getTeam().getName());
				mailerService.setMessage(sb.toString());
				mailerService.sendMail();
			}
			return this.list();
		}
		
		this.countryList = Utilities.getISOCountryList();
		this.userRoleList = this.userRoleService.list();
		this.teamList = this.teamService.list();
		return "add";
	}

}
